#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Project: pymysql3-2
# 实现菜单解析

import pymysql
import json


def extDictFromJson(jsonStr):
    dic = json.loads(jsonStr)
    return dic


def insertTarget(tgCursor, sql):
    try:
        tgCursor.execute(sql)
        tgDb.commit()
    except Exception as e:
        tgDb.rollback()
        print("Error: unable to fecth data:" + str(e))


tgDb = pymysql.connect(host="localhost",
                       user="mall",
                       password="mall",
                       port=3306,
                       database="mall",
                       charset='utf8')

tgCursor = tgDb.cursor()

f = open("D:\\iProject\\myPython\com\\teradata\\db\pymysql3\\data\\category.json", 'r+', encoding='UTF-8')
str_json = f.read()
temp = str_json.replace("'", '"')  # 将 单引号 替换为 双引号
temp = json.loads(temp)  # loads 将 字符串 解码为 字典

if temp['code'] == "0" and temp['message'] == 'success':
    # print(temp['data'])
    # print()
    # print("==============> id, pid, name, comment, image")
    for itemCategory in temp['data']:
        # print("CATEGORY MAIN => {}, {}, {}, {}, {}".format(itemCategory['mallCategoryId'],
        #                                                None,
        #                                                itemCategory['mallCategoryName'],
        #                                                itemCategory['comments'],
        #                                                itemCategory['image']
        #                                                ))
        sql = "insert into goods_categroy values('{}', '{}', '{}', '{}', '{}', 1)".format(itemCategory['mallCategoryId'],
                                                           None,
                                                           itemCategory['mallCategoryName'],
                                                           itemCategory['comments'],
                                                           itemCategory['image']
                                                           )
        print(sql)
        insertTarget(tgCursor, sql)

        if 'bxMallSubDto' in itemCategory:
            for subCategroy in itemCategory['bxMallSubDto']:
                # print("CATEGROY SUBS ==> {}, {}, {}, {}, {}".format(subCategroy['mallSubId'],
                #                                                 subCategroy['mallCategoryId'],
                #                                                 subCategroy['mallSubName'],
                #                                                 subCategroy['comments'],
                #                                                 None
                #                                                 ))
                sql = "insert into goods_categroy values('{}', '{}', '{}', '{}', '{}', 2)".format(subCategroy['mallSubId'],
                                                                subCategroy['mallCategoryId'],
                                                                subCategroy['mallSubName'],
                                                                subCategroy['comments'],
                                                                None
                                                                )
                print(sql)
                insertTarget(tgCursor, sql)

tgCursor.close()
tgDb.close()
